﻿using SqlSugar;
using System.Linq.Expressions;
using System.Reflection;
using WareHouse.Domain;

namespace WareHouse.Repositories.Repository
{
    public class Repository<TEntity> : IRepository<TEntity> where TEntity : class, new()
    {
        private readonly ISqlSugarClient db;
        public Repository(ISqlSugarClient db)
        {
            this.db = db;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual async Task<int> InsertAsync(TEntity entity)
        {
            //反射的应用
            Type type = entity.GetType();
            PropertyInfo[] properties = type.GetProperties();
            //如果当前实体包含创建人信息的审计字段，动态赋值
            foreach (PropertyInfo property in properties)
            {
                if (property.Name == nameof(AuditInfo.AddId))
                {
                    property.SetValue(entity, 666);
                }
                if (property.Name == nameof(AuditInfo.AddPerson))
                {
                    property.SetValue(entity, "张三");
                }
                if (property.Name == nameof(AuditInfo.AddDate))
                {
                    property.SetValue(entity, DateTime.Now);
                }
            }

           return  db.Insertable(entity).ExecuteCommand();
        }

        /// <summary>
        /// 查单条
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public virtual async Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> expression)
        {
            var eneity = await db.Queryable<TEntity>().FirstAsync(expression);
            return eneity;
        }

        /// <summary>
        /// 查全部
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public virtual async Task<List<TEntity>> QueryableAsync()
        {
            var eneity = await db.Queryable<TEntity>().ToListAsync();
            return eneity;
        }

        /// <summary>
        /// 按条件查询
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public virtual async Task<List<TEntity>> QueryableAsync(Expression<Func<TEntity, bool>> expression)
        {
            var eneity = await db.Queryable<TEntity>().Where(expression).ToListAsync();
            return eneity;
        }

        /// <summary>
        /// 单条修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual async Task<TEntity> UpdateAsync(TEntity entity)
        {
            //反射的应用
            Type type = entity.GetType();
            PropertyInfo[] properties = type.GetProperties();
            //如果当前实体包含创建人信息的审计字段，动态赋值
            foreach (PropertyInfo property in properties)
            {
                if (property.Name == nameof(AuditInfo.UpdId))
                {
                    property.SetValue(entity, 666);
                }
                if (property.Name == nameof(AuditInfo.UpdPerson))
                {
                    property.SetValue(entity, "张三");
                }
                if (property.Name == nameof(AuditInfo.UpdDate))
                {
                    property.SetValue(entity, DateTime.Now);
                }
            }

            await db.Updateable(entity).ExecuteCommandAsync();
            return entity;
        }

        /// <summary>
        /// 物理删除+逻辑删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual async Task<int> DeleteAsync(TEntity entity)
        {
            //反射的应用
            Type type = entity.GetType();
            PropertyInfo[] properties = type.GetProperties();
            var num = 0;
            //如果当前实体包含创建人信息的审计字段，动态赋值
            foreach (PropertyInfo property in properties)
            {
                if (property.Name == nameof(AuditInfo.DelId))
                {
                    property.SetValue(entity, 666);
                }
                if (property.Name == nameof(AuditInfo.DelPerson))
                {
                    property.SetValue(entity, "张三");
                }
                if (property.Name == nameof(AuditInfo.DelDate))
                {
                    property.SetValue(entity, DateTime.Now);
                }
                if (property.Name == nameof(AuditInfo.IsDel))
                {
                    num++;
                    property.SetValue(entity, false);
                }
            }

            // 当前实体没有IsDelete字段
            if (num == 0)
            {
                return await db.Deleteable(entity).ExecuteCommandAsync();
            }

            // 逻辑删除保存修改
            return await db.Updateable(entity).ExecuteCommandAsync();
        }

        /// <summary>
        /// 查单条
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public virtual async Task<TEntity> FirstAsync()
        {
            return await db.Queryable<TEntity>().FirstAsync();
        }
    }
}
